var gulp = require('gulp');
var gulpif = require('gulp-if');
var rev = require('gulp-rev');
var revCollector = require('gulp-rev-collector');
var imageisux = require('gulp-imageisux');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant');
var imageminMozjpeg = require('imagemin-mozjpeg');
var cache = require('gulp-cache');
var livereload = require('gulp-livereload');
var config = require('../config').images;

//基本使用
gulp.task('images', function () {
    return gulp.src(config.src)
        //.pipe(gulpif(global.isProd, imagemin()))
        .pipe(gulpif(global.isProd, rev()))
        .pipe(gulp.dest(config.dest))
        .pipe(gulpif(global.isProd, rev.manifest()))
        .pipe(gulpif(global.isProd, gulp.dest(config.rev)))
        //.pipe(imageisux('/', false))
        .pipe(livereload())
});

//其他参数
gulp.task('images-1', function () {
    return gulp.src(config.src)
        .pipe(imagemin({
            optimizationLevel: 5, //类型：Number  默认：3  取值范围：0-7（优化等级）
            progressive: true, //类型：Boolean 默认：false 无损压缩jpg图片
            interlaced: true, //类型：Boolean 默认：false 隔行扫描gif进行渲染
            multipass: true //类型：Boolean 默认：false 多次优化svg直到完全优化
        }))
        .pipe(gulp.dest(config.dest))
        .pipe(livereload())
});

//深度压缩图片
gulp.task('images-2', function () {
    return gulp.src(config.src)
        .pipe(imagemin({
            progressive: true,
            svgoPlugins: [{removeViewBox: false}],//不要移除svg的viewbox属性
            use: [pngquant()] //使用pngquant深度压缩png图片的imagemin插件
        }))
        .pipe(gulp.dest(config.dest))
        .pipe(livereload())
});

//只压缩修改的图片
gulp.task('images-3', function () {
    return gulp.src(config.src)
        .pipe(cache(imagemin({
            optimizationLevel: 3,
            progressive: true,
            interlaced: true,
            //multipass: true,
            //svgoPlugins: [{removeViewBox: false}],
            use: [
                imageminMozjpeg({quality: 80}),
                pngquant({quality: '65-80', speed: 4})
            ]
        })))
        .pipe(gulp.dest(config.dest))
        .pipe(livereload())
});